package com.hyw.cm.protocal.driver.xiaoan;

import org.springframework.util.Assert;

import com.hyw.cm.common.protocal.spi.IDecoder;
import com.hyw.cm.protocal.slow.SlowAttackLengthFieldBasedFrameDecoder;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.ByteToMessageDecoder;

public class XiaoAnDecoder implements IDecoder<ByteToMessageDecoder> {

  private static final int HEAD_LENGTH = 2; // 默认读取前二个字节
  private static final int HEAD_FLAG = 0xAA55;// 请求头标识

  @Override
  public int getIdentifyLength() {
    return HEAD_LENGTH;
  }

  @Override
  public boolean isSupport(byte[] identifier) {
    Assert.isTrue(identifier.length == HEAD_LENGTH, "illgal identifier");
    return Unpooled.wrappedBuffer(identifier).readUnsignedShort() == HEAD_FLAG;
  }

  @Override
  public ByteToMessageDecoder create() {
    return new SlowAttackLengthFieldBasedFrameDecoder(1024 * 1024, 4, 2, 0, 0, true);
  }

}
